using UnityEngine;
using System.Collections;

/// \class  gkCharacterControllerPhysicComponent
/// \brief  Physic component for actor with character controller
[AddComponentMenu("GK/Engine/Core/Physic/gkCharacterControllerPhysicController")]
[RequireComponent (typeof (CharacterController))]
public class gkCharacterControllerPhysicController : gkPhysicController
{
    /// The character controller
    private CharacterController m_rCharacterController;
	
	/// The main physic component
	private gkPhysicComponent m_rMainPhysicComponent;

    /// \brief  Called at level start
    void Start()
    {
        // Grab the character controller
        m_rCharacterController = GetComponent<CharacterController>();
		
		// Grab the main physic component
		m_rMainPhysicComponent = GetComponent<gkPhysicComponent>();
    }

    /// \brief  Make the linear movement
    protected override void ProcessLinearMovement()
    {
	    Vector3 f3PositionBeforeMovement;
		Vector3 f3RealLinearVelocity;
    	
	    // Save the position before the movement
	    f3PositionBeforeMovement = transform.position;

	    // Movement
        m_rCharacterController.Move(GetLinearVelocity() * Time.deltaTime);

	    // Compute the real velocity 
        f3RealLinearVelocity = (transform.position - f3PositionBeforeMovement) / Time.deltaTime;
		m_rMainPhysicComponent.SetLinearVelocity(f3RealLinearVelocity);
    }

    // \brief  Make the angular movement
    protected override void ProcessAngularMovement()
    {
	    // Movement
        transform.Rotate(GetAngularVelocity() * Time.deltaTime);
    }
}